﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Snowdreamist.Text.UBB
{
    public enum ListItemType
    {
        Default,
        Number,
        LowerCaseLetter,
        CapitalLetter
    }

    internal class ListFactory : PrefixUBBElementFactory
    {
        public ListFactory() : base(ElementPrefix) { }

        public override UBBElement Create(UBBContent ubbContent)
        {
            var matchResult = ElementRegex.Match(ubbContent.Text);
            if (!matchResult.Success)
                return null;

            if (matchResult.Groups.Count == 1)
            {
                return new ListElement();
            }
            else
            {
                char type = matchResult.Groups[1].Value[1];
                switch (type)
                {
                    case '1':
                        return new ListElement(ListItemType.Number);

                    case 'a':
                        return new ListElement(ListItemType.LowerCaseLetter);

                    case 'A':
                        return new ListElement(ListItemType.CapitalLetter);

                    default:
                        return null;
                }
            }
        }

        #region private

        private readonly static string ElementPrefix = "list";

        private readonly static Regex ElementRegex = new Regex("list(=[1aA])?", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Compiled);

        #endregion private
    }

    public class ListElement : UBBElement
    {
        public const string Label = "list";

        /// <summary>
        /// ctor
        /// </summary>
        public ListElement() : this(ListItemType.Default) { }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="itemType"></param>
        public ListElement(ListItemType itemType)
            : base(Label)
        {
            this.ItemType = itemType;
        }

        /// <summary>
        /// list item type
        /// </summary>
        public ListItemType ItemType { get; private set; }

        public override string SerializeToUBBCode()
        {
            switch (this.ItemType)
            {
                case ListItemType.Number:
                    return Label + "=1";

                case ListItemType.LowerCaseLetter:
                    return Label + "=a";

                case ListItemType.CapitalLetter:
                    return Label + "=A";

                default:
                case ListItemType.Default:
                    return Label;
            }
        }

        public override object Clone()
        {
            return new ListElement(this.ItemType);
        }
    }
}